<template>
  <el-dialog title="选择课程" :visible.sync="visible" width="90%" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="120px">
      <el-form-item label="课程包名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入课程包名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="courseList"  @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="课程包名称" align="center" prop="name" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectCourse">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listCourse
  } from "@/api/bus_web/course/course";
  import {
    manCourse
  } from "@/api/bus_web/school/schoolinfo";
export default {
  props: {
    id: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      courseIdList: [],
      // 总条数
      total: 0,
      // 未分配学生数据
      courseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        state: 0,
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      console.log(this.id)
      this.queryParams.excludeSchoolId = this.id;
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.courseIdList = selection.map(item => item.id);
    },
    // 查询表数据
    getList() {
     this.loading = true;
     listCourse(this.queryParams).then(response => {
       this.courseList = response.rows;
       this.total = response.total;
       this.loading = false;
     });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权用户操作 */
    handleSelectCourse() {
      const id = this.queryParams.excludeSchoolId;
      if (this.courseIdList.length<1) {
        this.$modal.msgError("请选择要分配的课程");
        return;
      }
      manCourse({ id: this.queryParams.excludeSchoolId, op:1,courseIdList: this.courseIdList }).then(res => {
        this.$modal.msgSuccess(res.message);
        this.visible = false;
        this.$emit("ok");
      });
    }
  }
};
</script>
